package rpcserver

import (
	rpcv1 "gluenet/internal/agent/api/rpc/v1"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver/rpcserver"
	"gluenet/internal/agent/service/jaegersvc"
	"gluenet/utils/rpc/v2/define"
)

const (
	jaeger = rpcv1.RPC_SERVICE_JAEGER
)

func init() {
	depend.Provide(NewJaegerRouter)
	registerRouter(jaeger, func(c rpcserver.IRoutes) {
		depend.Invoke(func(d *jaegerRouter) {
			d.initRouter(c)
		})
	})
}

type jaegerRouter struct {
	jaeger *jaegersvc.Service
}

func NewJaegerRouter(svc *jaegersvc.Service) *jaegerRouter {
	return &jaegerRouter{svc}
}

func (r *jaegerRouter) initRouter(c rpcserver.IRoutes) {
	c.Group(`jaeger`).
		Func(`service`, r.jaegerService).
		Func(`operate`, r.jaegerOperate).
		Func(`traces`, r.jaegerTraces)
}

func (r *jaegerRouter) jaegerService(c *rpcserver.Context) {
	jaegerServiceReq := new(define.JaegerService)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{jaegerServiceReq}); err != nil {
		return
	}
	resp := r.jaeger.JaegerService(*jaegerServiceReq)
	c.JSON(resp)
}

func (r *jaegerRouter) jaegerOperate(c *rpcserver.Context) {
	jaegerOperateReq := new(define.JaegerOperate)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{jaegerOperateReq}); err != nil {
		return
	}
	resp := r.jaeger.JaegerOperate(*jaegerOperateReq)
	c.JSON(resp)
}

func (r *jaegerRouter) jaegerTraces(c *rpcserver.Context) {
	jaegerTracesReq := new(define.JaegerTraces)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{jaegerTracesReq}); err != nil {
		return
	}
	resp := r.jaeger.JaegerTraces(*jaegerTracesReq)
	c.JSON(resp)
}
